#include <stdio.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/select.h>


int main(int argc, char const *argv[])
{
    // 创建一个客户端
    int client = socket(AF_INET, SOCK_STREAM, 0);
    if (client < 0)
    {
        perror("socket");
        return 1;
    }

    // 创建一个服务器地址
    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(8080);
    server_addr.sin_addr.s_addr = inet_addr("192.168.63.60");

    // 连接服务器
    if (connect(client, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
    {
        perror("connect");
        return 1;
    }
    
    // 定义一个描述符集合
    fd_set readfds = {0};
    while (1)
    {
        FD_SET(client, &readfds);
        select(client + 1, &readfds, NULL, NULL, NULL);
        if (FD_ISSET(client, &readfds))
        {
            char buf[1024] = {0};
            int len = read(client, buf, sizeof(buf));
            if (len < 0)
            {
                perror("read");
                return 1;
            }
            else if (len == 0)
            {
                printf("服务器断开连接\n");
                break;
            }
            else
            {
                printf("%s\n", buf);
            }
        }
    }
    return 0;
}
